import Taro from '@tarojs/taro'
/**
* 冷却函数：用于装饰重要的用户行为事件方法，防止因为网络卡顿或者连续点击造成后台数据不一致
* @mills 冷却时间：单位毫秒
* @param {*} target 
* @param {*} name 
* @param {*} descriptor 
*/
export function cd(mills = 500) {
   return (target, name, descriptor) => {
   var oldValue = descriptor.value
   let lastTime = 0
   descriptor.value = function () {
       let nowTime = new Date().getTime()
       if (nowTime - lastTime > mills) {
       lastTime = nowTime
       return oldValue.apply(this, arguments)
       }
   }
   return descriptor
   }
}

export function getSafeAreaTop() {
    let safeAreaTop = 0
    let systemInfo = Taro.getSystemInfoSync()
    console.log(systemInfo)
    // let safeArea = Taro.get
    const platform = systemInfo.platform
    if (platform === 'ios') {
      /** 真实环境：ios苹果系统不论实际手机有没有刘海屏，safeAreaTop 都会大于0 */
      safeAreaTop = systemInfo.safeArea.top
    } else if (platform === 'android') {
      /** 真实环境：andoird系统实际手机有刘海屏safeAreaTop>0 ,没有刘海屏 safeAreaTop=0  */
      safeAreaTop = systemInfo.safeArea.top
      if (safeAreaTop === 0) {
        safeAreaTop = systemInfo.statusBarHeight
      }
    } else if (platform === 'devtools') {
      /** 小程序开发工具里，无论对应的真机型号是否有刘海，safeAreaTop > 0 */
      safeAreaTop = systemInfo.safeArea.top
    }
    return safeAreaTop
}